package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Bill;
import com.fudan.backend.pojo.statics.AccountStatic;
import com.fudan.backend.service.BillService;
import com.fudan.backend.util.JwtUtil;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    BillService billService;

    @GetMapping("/personal")
    public R getUserBill(Integer current, Integer size, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getAllBillPageByUserId(userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/personal/week")
    public R getUserLastWeekBill(Integer current, Integer size, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getLastWeekBillPageByUserId(userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/personal/month")
    public R getUserLastMonthBill(Integer current, Integer size, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getLastMonthBillPageByUserId(userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/store/week/{storeId}")
    public R getStoreLastWeekBill(Integer current, Integer size, @PathVariable("storeId") String storeId, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getLastWeekBillPageByStoreId(storeId, userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/store/{storeId}")
    public R getStoreBill(Integer current, Integer size, @PathVariable("storeId") String storeId, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getAllBillPageByStoreId(storeId, userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/store/month/{storeId}")
    public R getStoreLastMonthBill(Integer current, Integer size, @PathVariable("storeId") String storeId, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Bill> billPage = billService.getLastMonthBillPageByStoreId(storeId, userId, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/mall-profit/week")
    public R getWebshopLastWeekBill(Integer current, Integer size) {
        PageInfo<Bill> billPage = billService.getLastWeekBillPageByAccountId(AccountStatic.WEBSHOP_ACCOUNT_ID, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/mall-profit")
    public R getWebShopBill(Integer current, Integer size) {
        PageInfo<Bill> billPage = billService.getAllBillPageByAccountId(AccountStatic.WEBSHOP_ACCOUNT_ID, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/mall-profit/month")
    public R getWebShopLastMonthBill(Integer current, Integer size) {
        PageInfo<Bill> billPage = billService.getLastMonthBillPageByAccountId(AccountStatic.WEBSHOP_ACCOUNT_ID, current, size);
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("billList", billPage.getList());
        retMap.put("total", billPage.getTotal());
        return new R(RetCode.BILL_SUCCESS, retMap);
    }

    @GetMapping("/personal/statistics/{type}")
    public R getUserStatistic(@PathVariable("type") int type, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        Map<String, Object> map = billService.calUserStatistic(userId, type);
        return new R(RetCode.BILL_SUCCESS, map);
    }

    @GetMapping("/store/{storeId}/statistics/{type}")
    public R getUserStatistic(@PathVariable("storeId") String storeId, @PathVariable("type") int type, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        Map<String, Object> map = billService.calStoreStatistic(storeId, userId, type);
        return new R(RetCode.BILL_SUCCESS, map);
    }

    @GetMapping("/mall-profit/statistics/{type}")
    public R getWebShopStatistic(@PathVariable("type") int type) {
        Map<String, Object> map = billService.calWebshopStatistic(type);
        return new R(RetCode.BILL_SUCCESS, map);
    }

}
